package com.it.test;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",1000);
        System.out.println("服务器已经连接成功");
        //搭建页面框架
        System.out.println("=========欢迎来到小周聊天室=========");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("请选择选项：");
        Scanner sc = new Scanner(System.in);
        while(true){
            switch(sc.nextInt()){
                case 1 -> login(socket);
                case 2 -> System.out.println("用户选择了注册");
                default -> System.out.println("没有这个选项");
            }
        }
    }

    public static void login(Socket socket) throws IOException {
        //获取输出流
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String  password= sc.nextLine();

        //拼接
        //username=zhangsan&password=123
        StringBuilder sb = new StringBuilder();
        sb.append("username=").append(username).append("&password=").append(password);

        //第一次写入执行登录操作（告诉服务器）
        bw.write("login");
        bw.newLine();
        bw.flush();
        //第二次写入数据
        bw.write(sb.toString());
        bw.newLine();
        bw.flush();

        //创建输入流
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String s = br.readLine();
        //可以用状态码代表传输的文字，比较方便
        //1:登陆成功 2:密码错误 3:用户不存在
        if(s.equals("1")){
            System.out.println("登录成功，可以开始聊天");
            //单独开一条线程去接收服务器发送过来的聊天记录
            new Thread(new ClientMyRunnable(socket)).start();
            //发出消息,显示在控制台
            talk2All(bw);
        }else if(s.equals("2")){
            System.out.println("密码有误，请重新登录");
        }else if(s.equals("3")){
            System.out.println("用户名不存在，请先注册");
        }
    }

    private static void talk2All(BufferedWriter bw) throws IOException {
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("请输入你要说的话");
            String message = sc.nextLine();
            bw.write(message);
            bw.newLine();
            bw.flush();
        }
    }
}
class ClientMyRunnable implements Runnable{
    Socket socket;
    public ClientMyRunnable(Socket socket){
        this.socket = socket;
    }
    @Override
    public void run() {
        //循环，重复的接收
        while(true) {
            try {
                //接收服务器发来的聊天记录
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String mag = br.readLine();
                System.out.println(mag);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
